package com.xt.mvcpilot.controller;

import java.beans.PropertyEditorSupport;
import java.util.List;

import com.xt.mvcpilot.data.Role;
import com.xt.mvcpilot.service.UserService;

public class RoleEditor extends PropertyEditorSupport {
	 
    private final UserService userService;
 
    public RoleEditor(UserService userService, Class collectionType) {
        super(collectionType);
        this.userService = userService;
    }
 
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        Object obj = getValue();
        List list = (List) obj;
        for (String str : text.split(",")) {
        	Role role = userService.getRole(Long.valueOf(str)); 
            list.add(role);
        }
    }
 
    @Override
    public String getAsText() {
        return super.getAsText();
    }
}